package com.atguigu.eduservice.controller;

/**
 * @author cyt
 * @create 2020-05-06 21:58
 */


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.subject.oneSubject;
import com.atguigu.eduservice.entity.subject.twoSubject;
import com.atguigu.eduservice.service.EduSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Api(description = "课程分类管理")
 //跨域
@RestController
@RequestMapping("/eduservice/subject")
public class SubjectAdminController {

    @Autowired
    private EduSubjectService subjectService;

    //添加课程分类
    @ApiOperation(value = "Excel批量导入")
    @PostMapping("addSubject")
    public R addSubject(MultipartFile file) {
        //1 获取上传的excel文件 MultipartFile
        //返回错误提示信息
        subjectService.importSubjectData(file, subjectService);
        //判断返回集合是否为空
        return R.ok();
    }

    @GetMapping("/getAllSubject")
    public R getAllSubject() {
        // 查出一级
        List<EduSubject> oneSubject = subjectService.getOneSubject();
        // 查出二级
        List<EduSubject> twoSubject = subjectService.getTwoSubject();
        // 封装一级
        System.out.println(oneSubject.size());
        System.out.println(twoSubject.size());
        ArrayList<oneSubject> list = new ArrayList<>();

        for (EduSubject subject : oneSubject) {
            oneSubject one = new oneSubject();
            BeanUtils.copyProperties(subject, one);
            List<twoSubject> finaltwo = new ArrayList<twoSubject>();
            for (EduSubject eduSubject : twoSubject) {
                if (one.getId().equals(eduSubject.getParentId())) {
                    twoSubject two = new twoSubject();
                    BeanUtils.copyProperties(eduSubject, two);
                    finaltwo.add(two);
                    System.out.println(finaltwo.toString());
                }
            }
            one.setChildren(finaltwo);
            list.add(one);
        }

        return R.ok().data("date", list);
    }
}
